package com.aaa.mrcai.controller;

import com.aaa.mrcai.annotion.AAALog;
import com.aaa.mrcai.pojo.entity.Dept;
import com.aaa.mrcai.pojo.vo.DepartVO;
import com.aaa.mrcai.query.DeptQuery;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.DepartService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author 惠普
 * @date 2021/12/2  20:47
 */
@RestController
@RequestMapping("depart")
public class DepartController {

    @Autowired
    private DepartService departService;

    @RequestMapping("list")
    public AjaxResult list(DeptQuery query){

        return departService.list(query);
    }

    /*导入学生信息上传*/
    @RequestMapping("uploadDeptExcel")
    public AjaxResult uploadDeptExcel(MultipartFile d) throws Exception {

        /*读取所有要导入的部门数据*/
        ExcelReaderBuilder read = EasyExcel.read(d.getInputStream());
        /*将数据转成Map形式*/
        List<Map<String, Object>> objects = read.doReadAllSync();

        /*调用service,将数据存到数据库中*/

        DepartVO departVO = new DepartVO();
        for (Map<String, Object> m : objects) {
            departVO.setDid(Integer.parseInt((String) m.get(0)));
            departVO.setType((String) m.get(1));
            departVO.setDname((String) m.get(2));
            departVO.setOther((String) m.get(3));
            departVO.setStartdate((String) m.get(4));
            departVO.setSid(Integer.parseInt((String) m.get(5)));
            AjaxResult ajaxResult = departService.addDepart(departVO);
            if (ajaxResult.getCode() == 2){
                return AjaxResult.defeated("导入中断，请检查信息是否有误");
            }
        }
        return AjaxResult.success("导入成功");
    }

    @RequestMapping("download")
    public void downloadInfo(DeptQuery query, HttpServletResponse resp) throws Exception{
        //调用service，查询符合条件的学生
        AjaxResult list = departService.list(query);
        Map<String,Object> data = (Map<String, Object>) list.getData();
        List<Dept> pageData = (List<Dept>) data.get("pageData");

        // 2 下载excel

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman

        resp.setContentType("application/vnd.ms-excel");
        resp.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("部门信息", "UTF-8");
        resp.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");


        EasyExcel.write(   resp.getOutputStream()  , Dept.class   )     .sheet("学生信息").doWrite( pageData );


    }
    @RequestMapping("listSubDepart")
    public AjaxResult listSubDepart(DeptQuery query){

        return departService.listSubDepart(query);
    }
    @RequestMapping("listSupDepart")
    public AjaxResult listSupDepart(){

        return departService.listSupDepart();
    }

    @RequestMapping("listType")
    public AjaxResult listType(){

        return departService.listType();
    }

    @RequestMapping("listTree")
    public AjaxResult listTree(){

        return departService.listTree();
    }

    @RequestMapping("/getDepart/{id}")
    public AjaxResult getDepart(@PathVariable("id") int id){

        return departService.getDepart(id);
    }

    @RequestMapping("update")
    @AAALog("修改部门")
        public AjaxResult updateDepart(DepartVO dept){

        return departService.updateDepart(dept);
    }

    @RequestMapping("listState")
    public AjaxResult listState(){
        return departService.listState();
    }

    @RequestMapping("addDepart")
    @AAALog("添加部门")
    public AjaxResult addDepart(DepartVO depart){
        return departService.addDepart(depart);
    }

    @RequestMapping("/delete/{id}")
    @AAALog("删除部门")
    public AjaxResult delDepart(@PathVariable("id") int id){
        return departService.delDepart(id);
    }

}
